#include "../tools.h"
#include <climits>

// 中序遍历 - 是否有序
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if(root == nullptr) return true;

        TreeNode* cur = root;
        TreeNode* mostR = nullptr;
        long long pre = LONG_MIN;
        bool flag = true;
        while(cur != nullptr){
            mostR = cur->left;
            if(mostR != nullptr){
                while(mostR->right != nullptr && mostR->right != cur) mostR = mostR->right;
                if(mostR->right == nullptr){
                    mostR->right = cur;
                    cur = cur->left;
                    continue;
                }
                else{
                    mostR->right = nullptr;
                }
            }

            if(cur->val <= pre) flag = false;
            pre = cur->val;
            cur = cur->right;
        }
        return flag;
    }
};


int main(int argc, char const *argv[])
{
    Solution sol;

    vector<int> vec = {5,1,4,-1,-1,3,6};
    TreeNode* tree = create_tree(vec);
    show(tree);

    cout << sol.isValidBST(tree);


    return 0;
}
